package com.ymatou.iapi.pushbind.testcase;

import com.ymatou.iapi.pushbind.parameter.BindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.ClearBindClientBean;
import com.ymatou.iapi.pushbind.service.ClearBindClientCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PushCallService;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
*清理绑定的客户端
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ClearBindClient{
	private static ClearBindClientBean clearbindclientBean;
	private static ClearBindClientCall clearbindclientCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("清理绑定的客户端");
	}

	@Before
	public void caseUp() {
		clearbindclientBean=new ClearBindClientBean();
		clearbindclientCall=new ClearBindClientCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(clearbindclientCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ClearBindClient_001() {
		Logger.start(true,"清理绑定的客户端");
		try {
			int userId = new Random().nextInt(10000);
			String deviceId1 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(-1);
			String clientId = "Test" + YMTDateUtil.getDate();
			BindUserClientBean bindUserClientBean = PushCallService.intitUserClient(userId);
			PushCallService.bindUserClient(bindUserClientBean);
			clearbindclientBean.setUserId(userId);
			clearbindclientBean.setUserSystemType(bindUserClientBean.getUserSystemType());
			clearbindclientCall.setData(clearbindclientBean);
			clearbindclientCall.callService();
			Logger.verifyEquals("200",clearbindclientCall.getString("Status"),"验证Status");
			ImDefaultWapper im = new ImDefaultWapper();
			List<Map> map = im.selectPushBind(clientId);
			Logger.verifyEquals(true,map.isEmpty(),"验证数据库数据为空");
			Logger.comment("验证redis数据");
			ImRedisWapper imRedisWapper = new ImRedisWapper();
			Set<String> set= imRedisWapper.getZrange(userId,"BindRecord:UserBindSet:1:"+userId);
			Logger.verifyEquals(true,set.isEmpty(),"验证redis数据为空");
			} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ClearBindClient_002() {
		Logger.start(true,"清理绑定的客户端，用户有多个绑定客户端");
		try {
			int userId = new Random().nextInt(10000);
			String deviceId1 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(-1);
			String deviceId2 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(3);
			String clientId1 = "Test" + YMTDateUtil.getDate();
			String clientId2 = "Test123" + YMTDateUtil.getDate();
			BindUserClientBean bindUserClientBean1 = PushCallService.intitUserClient(userId);
			BindUserClientBean bindUserClientBean2 = PushCallService.intitUserClient(userId);
			bindUserClientBean1.setDeviceId(deviceId1);
			bindUserClientBean1.setClientId(clientId1);
			bindUserClientBean2.setClientId(clientId2);
			bindUserClientBean2.setDeviceId(deviceId2);
			PushCallService.bindUserClient(bindUserClientBean1);
			PushCallService.bindUserClient(bindUserClientBean2);
			clearbindclientBean.setUserId(userId);
			clearbindclientBean.setUserSystemType(bindUserClientBean1.getUserSystemType());
			clearbindclientCall.setData(clearbindclientBean);
			clearbindclientCall.callService();
			Logger.verifyEquals("200",clearbindclientCall.getString("Status"),"验证Status");
			ImDefaultWapper im = new ImDefaultWapper();
			List<Map> map = im.selectPushBindByUserId(userId);
			Logger.verifyEquals(true,map.isEmpty(),"验证数据库数据为空");
			Logger.comment("验证redis数据");
			ImRedisWapper imRedisWapper = new ImRedisWapper();
			Set<String> set= imRedisWapper.getZrange(userId,"BindRecord:UserBindSet:1:"+userId);
			Logger.verifyEquals(true,set.isEmpty(),"验证redis数据为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ClearBindClient_003() {
		Logger.start(true,"清理绑定的客户端，userId为空");
		try {
			int userId = new Random().nextInt(10000);
			BindUserClientBean bindUserClientBean1 = PushCallService.intitUserClient(userId);
			PushCallService.bindUserClient(bindUserClientBean1);
			clearbindclientBean.setUserSystemType(bindUserClientBean1.getUserSystemType());
			clearbindclientCall.setData(clearbindclientBean);
			clearbindclientCall.callService();
			Logger.verifyEquals("406",clearbindclientCall.getString("Status"),"验证Status,参数错误");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


}